# Main
library(tidyverse)
library(lubridate)
# Visualization
library(tidyquant)
library(plotly)
bike_orderlines_tbl <- read_rds("00_data/bike_sales/data_wrangled/bike_orderlines.rds")total_sales_m_tbl <- bike_orderlines_tbl %>%
select(order_date, total_price) %>%
mutate(date_rounded = floor_date(order_date, unit = "month")) %>%
group_by(date_rounded) %>%
summarise(total_sales = sum(total_price)) %>% ungroup() %>%
# Create a lebl field
# Remeber anything in the brackets are R code
mutate(label_text = str_glue("Sales: {scales::dollar(total_sales)}
Date: {date_rounded %>% format('%B %Y')}"))"2011-01-01 00:00:00" %>% as_datetime() %>% format("")## [1] "2011-01-01"
g1 <- total_sales_m_tbl %>%
ggplot(aes(x = date_rounded, y = total_sales)) +
# Geoms
geom_point(aes(text = label_text), colour = "#2C3E50") +
geom_smooth(method = 'loess', span = 0.2) +
# formatting
theme_tq() +
scale_y_continuous(labels = scales::dollar_format(scale = 1e-6, suffix = "M")) +
labs(
title = "Total Sales",
y = "Revenue (USD)",
x = "Date"
)
g1ggplotly(g1, tooltip = "text")plot_total_sales <- function(unit = "month",
date_format = '%B %Y',
interactive = TRUE) {
# handle Data
data_tbl <- bike_orderlines_tbl %>%
select(order_date, total_price) %>%
mutate(date_rounded = floor_date(order_date, unit = unit)) %>%
group_by(date_rounded) %>%
summarise(total_sales = sum(total_price)) %>% ungroup() %>%
# Create a lebl field
# Remeber anything in the brackets are R code
mutate(label_text = str_glue("Sales: {scales::dollar(total_sales)}
Date: {date_rounded %>% format(date_format)}"))
# Make the plot
g <- data_tbl %>%
ggplot(aes(x = date_rounded, y = total_sales)) +
# Geoms
geom_point(aes(text = label_text), colour = "#2C3E50") +
geom_smooth(method = 'loess', span = 0.2) +
# formatting
theme_tq() +
scale_y_continuous(labels = scales::dollar_format(scale = 1e-6, suffix = "M")) +
labs(
title = "Total Sales",
y = "Revenue (USD)",
x = ""
)
# Static vs Interactive Logic
if (interactive) {
return(ggplotly(g, tooltip = "text"))
} else {
return(g)
}
}plot_total_sales(unit = "month", date_format = '%B %d, %Y', interactive = TRUE)category_2_sales_m_tbl <- bike_orderlines_tbl %>%
select(order_date, category_1, category_2, total_price) %>%
mutate(date_rounded = floor_date(order_date, unit = "month")) %>%
group_by(date_rounded, category_1, category_2) %>%
summarise(total_sales = sum(total_price)) %>% ungroup() %>%
mutate(label_text = str_glue("Sales: {scales::dollar(total_sales)}
Date: {date_rounded %>% format('%B %Y')}")) %>%
mutate(category_2 = as_factor(category_2) %>% fct_reorder2(date_rounded, total_sales))g2 <- category_2_sales_m_tbl %>%
ggplot(aes(date_rounded, total_sales, colour = category_2)) +
geom_point(aes(text = label_text)) +
geom_smooth(method = 'loess', span = 0.2) +
facet_wrap(~category_2, scales = "free_y", ncol = 3) +
# Protips: it is a good ideas to include expand_limits(y = 0)
# When plotting time series. This ensures the magnitude of the time series
# is porperly shown
expand_limits(y = 0) +
theme_tq() +
theme(legend.position = "none",
strip.text.x = element_text(margin = margin(5, 5, 5, 5, unit = "pt"))) +
scale_y_continuous(label = scales::dollar_format(scale = 1e-3, suffix = "K")) +
scale_colour_tq() +
labs(
title = "Bike Total Sales By Category 2",
x = "",
y = "Revenue (USD)",
)ggplotly(g2, tooltip = "text")plot_categories <- function(category_1 = "All", category_2 = "All",
unit = "month",
date_format = "%B %Y",
# n = number of columns
ncol = n,
scales = "free_y",
interactive = TRUE) {
# Handle the data
data_tbl <- bike_orderlines_tbl %>%
select(order_date, category_1, category_2, total_price) %>%
mutate(date_rounded = floor_date(order_date, unit = unit)) %>%
group_by(date_rounded, category_1, category_2) %>%
summarise(total_sales = sum(total_price)) %>% ungroup() %>%
mutate(label_text = str_glue("Sales: {scales::dollar(total_sales)}
Date: {date_rounded %>% format(date_format)}")) %>%
mutate(category_2 = as_factor(category_2) %>% fct_reorder2(date_rounded, total_sales))
# Handle Inputs
cat_1_text <- str_to_lower(category_1)
cat_2_text <- str_to_lower(category_2)
# Create filter logic
if(cat_1_text != "all"){
data_tbl <- data_tbl %>%
filter(category_1 %>%
str_to_lower() %>%
str_detect(pattern = cat_1_text))
}
if(cat_2_text != "all"){
data_tbl <- data_tbl %>%
filter(category_2 %>%
str_to_lower() %>%
str_detect(pattern = cat_2_text))
}
# make the plot
g2 <- data_tbl %>%
ggplot(aes(date_rounded, total_sales, colour = category_2)) +
geom_point(aes(text = label_text), colour = palette_light()[1]) +
geom_smooth(method = 'loess', span = 0.2) +
facet_wrap(~category_2, scales = scales, ncol = ncol) +
# Protips: it is a good ideas to include expand_limits(y = 0)
# When plotting time series. This ensures the magnitude of the time series
# is porperly shown
expand_limits(y = 0) +
theme_tq() +
theme(legend.position = "none",
strip.text.x = element_text(margin = margin(5, 5, 5, 5, unit = "pt"))) +
scale_y_continuous(label = scales::dollar_format(scale = 1e-3, suffix = "K")) +
scale_colour_tq() +
labs(
title = "Bike Total Sales By Category 2",
x = "",
y = "",
)
# Create Static VS Interactive Logic
if (interactive) {
return(ggplotly(g2, tooltip = "text"))
} else {
return(g2)
}
}plot_categories(category_1 = "all",
category_2 = "(cross country|Elite|Trail)",
unit = "month",
ncol = 1,
scales = "fixed",
interactive = TRUE)plot_categories(category_1 = "all",
category_2 = "all",
unit = "month",
ncol = 3,
date_format = "%Y-%m-%d",
interactive = TRUE)fs::file_create("01_Scripts/plot_sales.R")
dump(list = c("plot_total_sales", "plot_categories"), file = "01_Scripts/plot_sales.R")